﻿/****************************** Module Header ******************************\
 * Module Name:  UriInfomation.cs
 * Module Version:  1.0.0 (2012-8-10)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used for UriInfomation.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Web;
using System.Linq;

namespace UCF.Core.Web
{
    /// <summary>
    /// 应用程序路径信息对象 已弃用,推荐使用_Url
    /// </summary>
    public class UriInfomation
    {
        private static UriInfomation instance;
        public static UriInfomation Instance
        {
            get
            {
                if (instance==null || HttpContext.Current.Request.Url.Authority != instance.Domain) instance = new UriInfomation();
                return instance;
            }
        }

        private Lazy<string> filepath = new Lazy<string>(() => System.Web.HttpContext.Current.Request.FilePath);
        /// <summary>
        /// 返回当前请求的虚拟路径,相当于WebApplication的Path
        /// <para>如：http://t.cn/blog/read.aspx?id=1 返回 /blog/read.aspx </para>
        /// </summary>
        public string FilePath { get { return filepath.Value; } }

        private Lazy<string> applicationpath = new Lazy<string>(() => HttpContext.Current.Request.ApplicationPath);
        /// <summary>
        /// 返回当前应用程序的根目录路径
        /// <para>如：http://t.cn/read.aspx?id=1 返回 / </para>
        /// <para>如：http://t.cn/blog/read.aspx?id=1 返回 /blog </para>
        /// </summary>
        public string ApplicationPath { get { return applicationpath.Value; } }

        private Lazy<string> basefilename = new Lazy<string>(() => System.IO.Path.GetFileName(HttpContext.Current.Request.ApplicationPath));

        /// <summary>
        /// 返回当前执行脚本的页面名称
        /// <para>如：http://t.cn/read.aspx?id=1 返回 read.aspx </para>
        /// </summary>
        public string BaseFileName { get { return basefilename.Value; } }

        private Lazy<Uri> url = new Lazy<Uri>(() => HttpContext.Current.Request.Url);
        /// <summary>
        /// 获取URL信息
        /// <para>如：http://t.cn/read.aspx?id=1 返回 http://t.cn/read.aspx?id=1 </para>
        /// </summary>
        public Uri Url { get { return url.Value; } }

        private Lazy<string> domain = new Lazy<string>(() => HttpContext.Current.Request.Url.Authority);
        /// <summary>
        /// 获取主机域名信息
        /// <para>如：http://t.cn/read.aspx?id=1 返回 t.cn </para>
        /// </summary>
        /// <returns></returns>
        public string Domain { get { return domain.Value; } }

        private Lazy<string> absoluteUri = new Lazy<string>(() => HttpContext.Current.Request.Url.AbsolutePath);
        /// <summary>
        /// 获取URL信息
        /// <para>如：http://t.cn/read.aspx?id=1 返回 http://t.cn/read.aspx?id=1 </para>
        /// </summary>
        /// <returns></returns>
        public string AbsoluteUri { get { return absoluteUri.Value; } }

        private Lazy<Uri> urlReferrer = new Lazy<Uri>(() => HttpContext.Current.Request.UrlReferrer);
        /// <summary>
        /// 获取客户端上次请求的信息
        /// <para>如：http://t.cn/read.aspx?id=1 返回 http://t.cn/default.aspx </para>
        /// </summary>
        public Uri UrlReferrer { get { return urlReferrer.Value; } }

        private Lazy<string> pathAndQuery = new Lazy<string>(() => HttpContext.Current.Request.Url.PathAndQuery);
        /// <summary>
        /// 获取当前请求的虚拟路径(包含完整的参数信息)
        /// <para>如：http://t.cn/blog/read.aspx?id=1 返回 /blog/read.aspx?id=1 </para>
        /// </summary>
        public string PathAndQuery { get { return pathAndQuery.Value; } }

        private Lazy<System.Collections.Specialized.NameValueCollection> queryString = new Lazy<System.Collections.Specialized.NameValueCollection>(() => HttpContext.Current.Request.QueryString);
        /// <summary>
        /// 获取当前请求的查询字符串变量集合
        /// </summary>
        public System.Collections.Specialized.NameValueCollection QueryString { get { return queryString.Value; } }

        /// <summary>
        /// URI转换为路径
        /// <para>如 /a/b/c=>可点击的逐级路径</para>
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="format"></param>
        /// <param name="split"></param>
        /// <returns></returns>
        public static string ToLocation(string uri, string format = "<a href='?path={1}'>{0}</a>", string split = " / ")
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            string spath = uri;
            int len = 0;
            foreach (string s in uri.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries).Reverse())
            {
                spath = spath.Substring(0, spath.Length - len);
                len = s.Length + 1;
                sb.Insert(0, format.Fmt(s, spath) + split);
            }
            if (uri.StartsWith("/")) sb.Insert(0, format.Fmt("/", "/") + split);
            return sb.Remove(sb.Length - split.Length, split.Length).ToString().Replace("//", "/");
        }
    }
}
